package  
{
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2FixtureDef;
	import Box2D.Dynamics.b2World;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	/**
	 * 
	 * @author Andy
	 */
	public class Human extends GameObject 
	{
		private evar body:b2Body;
		private var sprite:Sprite;
		
		/**
		 * Create a new Human.
		 * @param	world		the Box2D world to add the body to
		 * @param	position	the position in the world to create the Human
		 */
		public function Human(world:b2World, position:b2Vec2) 
		{
			super();
			makeBody(world, position);
		}
		
		/**
		 * Creates the body in the physics engine.
		 * @param	world
		 */
		public function makeBody(world:b2World, position:b2Vec2):void
		{
			// A bodyDef defines what type of body to create, without actually
			// creating the body yet.
			var bodyDef:b2BodyDef = new b2BodyDef();
			bodyDef.position = new b2Vec2(10, 10);
			bodyDef.type = b2Body.b2_dynamicBody;	// dynamic moves, static does not
			bodyDef.linearDamping = 8.0;			// like air friction
			bodyDef.angularDamping = 20.0;			// like friction to slow rotation.
			
			// The shape is for collision detection
			var shape:b2PolygonShape = new b2PolygonShape();
			// For now, we will just make the shape a box
			shape.SetAsBox(1.0, 1.0);
			// The fixture is the link between the shape and the body.
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape = shape;
			// The mass density
			fixtureDef.density = 5.0;
			// The coefficient of friction for collisions with other bodies.
			fixtureDef.friction = 1.0;
			// User data lets us add a reference back to the object this shape
			// belongs to.
			fixtureDef.userData = this;
			
			body = world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			body.SetUserData(this);
		}
		
		/**
		 * Moves the person.
		 * @param	direction	The direction to move, in radians CCW from east
		 */
		public function move(direction:Number):void
		{
			// Create a vector in the direction of movement
			var impulse:b2Vec2 = new b2Vec2(Math.cos(direction),Math.sin(direction));
			// Apply the force to the body
			body.ApplyForce();
		}
		
		override public function render(surface:BitmapData):void 
		{
			// Create a transformation matrix.
			var matrix:Matrix = new Matrix();
			// Move that transformation matrix according to the person's position.
			matrix.translate(body.GetPosition().x, body.GetPosition().y);
			// Scale the matrix
			matrix.scale(10);
			
			// Draw the sprite on the surface using the matrix
			surface.draw(sprite, matrix);
		}
	}

}